<?php
namespace app\admin\controller;
use think\Db;
use lib\Curl;
use tcpdf\tcpdf;
class Orders extends Base{
    
    public function orders_lists(){
        $keyword=input('get.keyword');
        $pay_style=input('get.pay_style');
        $state=input('get.state');
        if (!empty(input('get.fromtime'))) {
            $fromtime=strtotime(input('get.fromtime'));
        }else{
            $fromtime=0;
        }
        if (!empty(input('get.totime'))) {
            $totime=strtotime(input('get.totime'))+86400;
        }else{
            $totime=time();
        }
        $map = [
            ['a.time', '>', $fromtime],
            ['a.time', '<', $totime]
        ];
        if($pay_style==0){
            array_push($map,['a.pay_style','<',3]);
        }else{
            array_push($map,['a.pay_style','=',$pay_style]);
        }
        if($state==0){
            array_push($map,['a.state','<',7]);
        }else{
            array_push($map,['a.state','=',$state]);
        }
        $list=Db::name('orders')->alias('a')->join('users b', 'a.user_id = b.id','LEFT')->join('product c', 'a.p_id = c.id','LEFT')->field('a.*,c.img,c.title')->where($map)->where('a.orderno|c.title','like',"%{$keyword}%")->order('a.id desc')->paginate(10);
        $count=Db::name('orders')->alias('a')->join('users b', 'a.user_id = b.id','LEFT')->join('product c', 'a.p_id = c.id','LEFT')->field('a.*,c.img,c.title')->where($map)->where('a.orderno|c.title','like',"%{$keyword}%")->count();
        
        $this->assign('keyword',input('get.keyword'));
        $this->assign('pay_style',$pay_style);
        $this->assign('state',$state);
        $this->assign('fromtime',input('get.fromtime'));
        $this->assign('totime',input('get.totime'));
        $this->assign('list',$list);
        $this->assign('count',$count);
        return $this->fetch();
    }

    public function order_details(){
        if(request()->isGet()){
            $param = input('get.');
            $data=Db::name('orders')->alias('a')->join('product b', 'a.p_id = b.id','LEFT')->field('a.*,b.img')->where('a.id',$param['id'])->find();
            $datas=Db::name('ordersub')->alias('a')->join('product b', 'a.p_id = b.id','LEFT')->join('orders c', 'a.orders_id = c.id','LEFT')->field('a.*,b.img,c.name,c.phone,c.address')->where('a.orders_id',$param['id'])->select();
            $this->assign('back','');
            $this->assign('data',$data);
            $this->assign('datas',$datas);
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['express_company']=$param['express_company'];
            $data['express_no']=$param['express_no'];
            $data['state']=3;
            $data['fhtime']=time();
            $data['uptime']=time();
            $res=Db::name('orders')->where(array('id'=>$param['id']))->update($data);
            if($res){
                    Db::name('ordersub')->where(array('orders_id'=>$param['id']))->update($data);
                    return json(['code' => 200, 'msg' => '成功！']);
            }else{
                return json(['code' => 500, 'msg' => '失败！']);
            }
            
        }
    }
    public function price_edits(){
        if(request()->isGet()){
            $param = input('get.');
            $data['id']=$param['id'];
            // $data['pay_fee']=Db::name('ordersub')->where('id',$param['id'])->value('pay_fee');
            $this->assign('data',$data);
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $f_pay_fee=Db::name('orders')->where('id',$param['id'])->value('f_pay_fee');
            $data['discount']=$param['discount'];
            $data['remarks']=$param['remarks'];
            $data['pay_fee']=bcmul($f_pay_fee,bcdiv($data['discount'],10,1),2);
            $data['uptime']=time();
            $res=Db::name('orders')->where(array('id'=>$param['id']))->update($data);
            if($res){
                $f_pay_fees=Db::name('ordersub')->where(array('orders_id'=>$param['id']))->select();
                foreach ($f_pay_fees as $key => $value) {
                    $datasub['discount']=$param['discount'];
                    $datasub['remarks']=$param['remarks'];
                    $datasub['pay_fee']=bcmul($f_pay_fees[$key]['f_pay_fee'],bcdiv($param['discount'],10,1),2);
                    $datasub['uptime']=time();
                    Db::name('ordersub')->where(array('id'=>$f_pay_fees[$key]['id']))->update($datasub);
                }
                    return json(['code' => 200, 'msg' => '成功！']);
            }else{
                return json(['code' => 500, 'msg' => '失败！']);
            }
            
        }
    }

    public function pay_edits(){
        $id=input('param.id');
        $status = Db::name('orders')->where(array('id'=>$id,'state'=>1))->update(['state'=>2]);
        if($status){
            Db::name('ordersub')->where(array('orders_id'=>$id,'state'=>1))->update(['state'=>2]);
            return json(['code' => 200,'msg' => '成功']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }
    public function invoicings(){
        $id=input('param.id');
        $status = Db::name('orders')->where(array('id'=>$id,'is_invoice'=>2))->update(['is_invoiced'=>2]);
        if($status){
            Db::name('ordersub')->where(array('orders_id'=>$id,'is_invoice'=>2))->update(['is_invoiced'=>2]);
            return json(['code' => 200,'msg' => '成功']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }
    public function confirm_orders(){
        $id=input('param.id');
        $status = Db::name('orders')->where(array('id'=>$id,'state'=>2))->update(['state'=>4]);
        if($status){
            Db::name('ordersub')->where(array('orders_id'=>$id,'state'=>2))->update(['state'=>4]);
            return json(['code' => 200,'msg' => '成功']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }
    public function orders_list(){
        $keyword=input('get.keyword');
        $pay_style=input('get.pay_style');
        $state=input('get.state');
        if (!empty(input('get.fromtime'))) {
            $fromtime=strtotime(input('get.fromtime'));
        }else{
            $fromtime=0;
        }
        if (!empty(input('get.totime'))) {
            $totime=strtotime(input('get.totime'))+86400;
        }else{
            $totime=time();
        }
        $map = [
            ['a.time', '>', $fromtime],
            ['a.time', '<', $totime]
        ];
        if($pay_style==0){
            array_push($map,['a.pay_style','<',3]);
        }else{
            array_push($map,['a.pay_style','=',$pay_style]);
        }
        $pay_fees=Db::name('ordersub')->alias('a')->field('a.*')->where($map)->where('a.state','in','2,3,4')->where('a.ordernosub|a.title','like',"%{$keyword}%")->sum('pay_fee');
        $f_pay_fees=Db::name('ordersub')->alias('a')->field('a.*')->where($map)->where('a.state','in','2,3,4')->where('a.ordernosub|a.title','like',"%{$keyword}%")->sum('f_pay_fee');
        if($state==0){
            array_push($map,['a.state','<',7]);
        }else{
            array_push($map,['a.state','=',$state]);
        }
        $list=Db::name('ordersub')->alias('a')->join('users b', 'a.user_id = b.id','LEFT')->join('product c', 'a.p_id = c.id','LEFT')->join('orders d', 'a.orders_id = d.id','LEFT')->field('a.*,c.img,d.name,d.phone')->where($map)->where('a.ordernosub|a.title','like',"%{$keyword}%")->order('a.id desc')->paginate(10);
        $count=Db::name('ordersub')->alias('a')->join('users b', 'a.user_id = b.id','LEFT')->join('product c', 'a.p_id = c.id','LEFT')->join('orders d', 'a.orders_id = d.id','LEFT')->field('a.*,c.img,d.name,d.phone')->where($map)->where('a.ordernosub|a.title','like',"%{$keyword}%")->count();
        
        $this->assign('keyword',input('get.keyword'));
        $this->assign('pay_style',$pay_style);
        $this->assign('state',$state);
        $this->assign('fromtime',input('get.fromtime'));
        $this->assign('totime',input('get.totime'));
        $this->assign('list',$list);
        $this->assign('count',$count);
        $this->assign('pay_fees',$pay_fees);
        $this->assign('f_pay_fees',$f_pay_fees);
        return $this->fetch();
    }
    public function order_detail(){
        if(request()->isGet()){
            $param = input('get.');
            $data=Db::name('ordersub')->alias('a')->join('product b', 'a.p_id = b.id','LEFT')->join('orders c', 'a.orders_id = c.id','LEFT')->field('a.*,b.img,c.name,c.phone,c.address')->where('a.id',$param['id'])->find();
            $back=Db::name('apply_back')->where('order_id',$param['id'])->where('user_id',$data['user_id'])->find();
            if($back){
                $this->assign('back',$back);
            }else{
                $this->assign('back','');
            }
            $this->assign('data',$data);
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['express_company']=$param['express_company'];
            $data['express_no']=$param['express_no'];
            $data['state']=3;
            $data['fhtime']=time();
            $data['uptime']=time();
            $res=Db::name('ordersub')->where(array('id'=>$param['id']))->update($data);
            if($res){
                    return json(['code' => 200, 'msg' => '成功！']);
            }else{
                return json(['code' => 500, 'msg' => '失败！']);
            }
            
        }
    }
    public function agree_on(){
        $id=input('param.id');
        $status = Db::name('apply_back')->where(array('id'=>$id,'state'=>1))->update(['state'=>2]);
        if($status){
            return json(['code' => 200,'msg' => '成功']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }
    public function money_on(){
        $id=input('param.id');
        $status = Db::name('apply_back')->where(array('id'=>$id,'state'=>3))->update(['state'=>5]);
        if($status){
            return json(['code' => 200,'msg' => '成功']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }
    public function confirm_order(){
        $id=input('param.id');
        $status = Db::name('ordersub')->where(array('id'=>$id,'state'=>2))->update(['state'=>4]);
        if($status){
            return json(['code' => 200,'msg' => '成功']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }
    public function price_edit(){
        if(request()->isGet()){
            $param = input('get.');
            $data['id']=$param['id'];
            // $data['pay_fee']=Db::name('ordersub')->where('id',$param['id'])->value('pay_fee');
            $this->assign('data',$data);
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $f_pay_fee=Db::name('ordersub')->where('id',$param['id'])->value('f_pay_fee');
            $data['discount']=$param['discount'];
            $data['remarks']=$param['remarks'];
            $data['pay_fee']=bcmul($f_pay_fee,bcdiv($data['discount'],10,1),2);
            $data['uptime']=time();
            $res=Db::name('ordersub')->where(array('id'=>$param['id']))->update($data);
            if($res){
                    return json(['code' => 200, 'msg' => '成功！']);
            }else{
                return json(['code' => 500, 'msg' => '失败！']);
            }
            
        }
    }
    public function pay_edit(){
        $id=input('param.id');
        $status = Db::name('ordersub')->where(array('id'=>$id,'state'=>1))->update(['state'=>2]);
        if($status){
            return json(['code' => 200,'msg' => '成功']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }
    public function invoicing(){
        $id=input('param.id');
        $status = Db::name('ordersub')->where(array('id'=>$id,'is_invoice'=>2))->update(['is_invoiced'=>2]);
        if($status){
            return json(['code' => 200,'msg' => '成功']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }
    public function order_del(){
        $param = input('post.');
        $res= Db::name('ordersub')->where(array('id'=>$param['id']))->update(['state'=>7]);
        return json(['code' => 200,'msg' => '删除成功']);

    }
    public function export_order() {
        $result=Db::name('ordersub')->select();
        foreach ($result as $key => $value) {
            $value['time']=date('Y-m-d H:i:s',$value['time']);
            $value['fktime']=date('Y-m-d H:i:s',$value['fktime']);
            $value['fhtime']=date('Y-m-d H:i:s',$value['fhtime']);
        }
        $xlsCell  = array(
            array('id','ID'),
            array('orders_id','OID'),
            array('user_id','用户ID'),
            array('cart_id','购物车ID'),
            array('p_id','产品ID'),
            array('title','产品标题'),
            array('spec_combination_id','参数ID'),
            array('combination','参数'),
            array('ordernosub','订单号'),
            array('price','单价'),
            array('buy_num','购买量'),
            array('pay_fee','支付费'),
            array('f_pay_fee','原支付费'),
            array('discount','折扣'),
            array('remarks','折扣备注'),
            array('message','留言'),
            array('express_style','配送方式(1:物流;2:自提;3:到付)'),
            array('express_company','快递公司'),
            array('express_no','快递单号'),
            array('is_invoice','发票(1:否;2:是)'),
            array('is_invoiced','发票(1:待开;2:已开)'),
            array('company_name','发票单位'),
            array('invoice_no','识别号'),
            array('state','状态(1:待支付;2:待发货;3:待收货;4:已完成(待评价);5:退款/售后;6取消;7假删除)'),
            array('pay_style','支付方式(1:支付宝2:微信)'),
            array('score','积分'),
            array('time','下单时间'),
            array('fktime','付款时间'),
            array('fhtime','发货时间')
        );
        $this->exportexcel('订单表',$xlsCell,$result);
    }

    public static function exportexcel($expTitle, $expCellName, $expTableData) {
        $xlsTitle = iconv('utf-8', 'gb2312', $expTitle);//文件名称
        $fileName = $xlsTitle;//or $xlsTitle 文件名称可根据自己情况设定
        $cellNum = count($expCellName);
        $dataNum = count($expTableData);

        $objPHPExcel = new \PHPExcel();
        $cellName = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ');
        // Miscellaneous glyphs, UTF-8
        for ($i = 0; $i < $cellNum; $i++) {
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i] .'1', $expCellName[$i][1]);
        }
        for ($i = 0; $i < $dataNum; $i++) {
            for ($j = 0; $j < $cellNum; $j++) {
                $objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j] . ($i + 2), ' '.$expTableData[$i][$expCellName[$j][0]]);
            }
        }
        header('pragma:public');
        header('Content-type:application/vnd.ms-excel;charset=utf-8;name="' . $xlsTitle . '.xls"');
        header("Content-Disposition:attachment;filename=$fileName.xls");//attachment新窗口打印inline本窗口打印
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');
        exit;
    }
    
}
